/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.siro.pm.ui.main;

import com.siro.pm.Constants;
import com.siro.pm.I18nManager;
import com.siro.pm.PmApplication;
import com.siro.pm.model.Developer;
import com.siro.pm.model.provider.DeveloperProvider;
import com.siro.pm.ui.detail.ResetPwdPage;
import com.siro.pm.ui.view.AdminView;
import com.siro.pm.ui.view.AttendanceView;
import com.siro.pm.ui.view.ProjectView;
import com.siro.pm.ui.view.UserView;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;

public class MainPage extends VerticalLayout {
	private static final long serialVersionUID = 1L;

	MainPage self;
	protected HorizontalLayout header;
	protected CssLayout central;
	protected HorizontalLayout footer;
	Developer user = (Developer) PmApplication.getDefault().getUser();

	public MainPage() {
		self = this;
		setSizeFull();
		initHeader();
		initMain();
		if (user.getRole().equals(DeveloperProvider.DEVELOPER_ROLE_DATA[1])) {
			AdminView adminPage = new AdminView();
			adminPage.createContents();
			setCentralContent(adminPage);
		} else {
			UserView userPage = new UserView();
			userPage.setMainPage(self);
			setCentralContent(userPage);
		}
		initFooter();
	}

	protected void initHeader() {
		header = new HorizontalLayout();
		header.setHeight("50px");
		header.setWidth("100%");
		header.setStyleName(Constants.STYLE_HEADER);
		// header.setSpacing(true);
		header.setMargin(false, true, false, false);

		// logo
		Component logo = createLogo();
		header.addComponent(logo);
		header.setComponentAlignment(logo, Alignment.MIDDLE_LEFT);

		// admin view
		if (user.getRole().equals(DeveloperProvider.DEVELOPER_ROLE_DATA[1])) {
			Component adminView = createAdminView();
			header.addComponent(adminView);
			header.setComponentAlignment(adminView, Alignment.BOTTOM_LEFT);
		} else {
			// user view
			Component userView = createUserView();
			header.addComponent(userView);
			header.setComponentAlignment(userView, Alignment.BOTTOM_LEFT);

			// project view
			Component projectsView = createProjectsView();
			header.addComponent(projectsView);
			header.setComponentAlignment(projectsView, Alignment.BOTTOM_LEFT);

			// attendance view
			// Component attendanceView = createAttendanceView();
			// header.addComponent(attendanceView);
			// header.setComponentAlignment(attendanceView,
			// Alignment.BOTTOM_LEFT);
		}

		// empty
		HorizontalLayout empty = new HorizontalLayout();
		header.addComponent(empty);
		header.setComponentAlignment(empty, Alignment.MIDDLE_CENTER);
		header.setExpandRatio(empty, 1);

		// user info
		Component userinfo = createUserInfo();
		header.addComponent(userinfo);
		header.setComponentAlignment(userinfo, Alignment.MIDDLE_RIGHT);

		// Breadcrumbs
		// header.addComponent(breadcrumbs);
		// header.setExpandRatio(breadcrumbs, 1);
		// header.setComponentAlignment(breadcrumbs, Alignment.MIDDLE_LEFT);

		addComponent(header);
	}

	protected void initMain() {
		central = new CssLayout();
		central.setSizeFull();
		central.addStyleName(Constants.STYLE_MAIN_CONTENT);
		addComponent(central);
		setExpandRatio(central, 1.0f);
	}

	protected void initFooter() {
		footer = new HorizontalLayout();
		footer.setWidth(100, UNITS_PERCENTAGE);
		footer.setStyleName(Constants.STYLE_MAIN_FOOTER);

		Label copyright = new Label(I18nManager.getMessage("footer.message"));
		footer.addComponent(copyright);
		footer.setComponentAlignment(copyright, Alignment.MIDDLE_CENTER);
		addComponent(footer);
	}

	private Component createLogo() {
		Button logo = new NativeButton();
		logo.setWidth(160, Sizeable.UNITS_PIXELS);
		logo.setDescription("http://siro-info.com");
		logo.setStyleName(BaseTheme.BUTTON_LINK);
		logo.addStyleName(Constants.STYLE_APPLICATION_LOGO);
		return logo;
	}

	private Component createAttendanceView() {
		VerticalLayout attendanceView = new VerticalLayout();
		attendanceView.setWidth(60, Sizeable.UNITS_PIXELS);
		Button button_User = new NativeButton();
		button_User.setDescription(I18nManager
				.getMessage("header.attendanceView"));
		button_User.setStyleName(BaseTheme.BUTTON_LINK);
		button_User.addStyleName(Constants.STYLE_HEADER_ATTENDANCEVIEW);
		button_User.addListener(new Button.ClickListener() {
			private static final long serialVersionUID = 8822385075590507168L;

			public void buttonClick(ClickEvent event) {
				AttendanceView attendancePage = new AttendanceView();
				attendancePage.createContents();
				setCentralContent(attendancePage);
			}
		});
		attendanceView.addComponent(button_User);
		attendanceView.setComponentAlignment(button_User,
				Alignment.BOTTOM_CENTER);
		return attendanceView;

	}

	private Component createUserView() {
		VerticalLayout userView = new VerticalLayout();
		userView.setWidth(60, Sizeable.UNITS_PIXELS);
		Button button_User = new NativeButton();
		button_User.setDescription(I18nManager.getMessage("header.userView"));
		button_User.setStyleName(BaseTheme.BUTTON_LINK);
		button_User.addStyleName(Constants.STYLE_HEADER_USERVIEW);
		button_User.addListener(new Button.ClickListener() {
			private static final long serialVersionUID = 8822385075590507168L;

			public void buttonClick(ClickEvent event) {
				UserView userPage = new UserView();
				userPage.setMainPage(self);
				setCentralContent(userPage);
			}
		});
		userView.addComponent(button_User);
		userView.setComponentAlignment(button_User, Alignment.BOTTOM_CENTER);
		return userView;
	}

	private Component createProjectsView() {
		VerticalLayout projectsView = new VerticalLayout();
		projectsView.setWidth(60, Sizeable.UNITS_PIXELS);
		Button button_Project = new NativeButton();
		button_Project.setDescription(I18nManager
				.getMessage("header.projectView"));
		button_Project.setStyleName(BaseTheme.BUTTON_LINK);
		button_Project.addStyleName(Constants.STYLE_HEADER_PROJECTVIEW);
		button_Project.addListener(new Button.ClickListener() {
			private static final long serialVersionUID = 8822385075590507168L;

			public void buttonClick(ClickEvent event) {
				ProjectView projectView = new ProjectView();
				projectView.createContents();
				setCentralContent(projectView);
			}
		});
		projectsView.addComponent(button_Project);
		projectsView.setComponentAlignment(button_Project,
				Alignment.BOTTOM_CENTER);
		return projectsView;
	}

	private Component createAdminView() {
		VerticalLayout projectsView = new VerticalLayout();
		projectsView.setWidth(60, Sizeable.UNITS_PIXELS);
		Button button_Admin = new NativeButton();
		button_Admin.setDescription(I18nManager.getMessage("header.adminView"));
		button_Admin.setStyleName(BaseTheme.BUTTON_LINK);
		button_Admin.addStyleName(Constants.STYLE_HEADER_ADMINVIEW);
		button_Admin.addListener(new Button.ClickListener() {
			private static final long serialVersionUID = 8822385075590507168L;

			public void buttonClick(ClickEvent event) {
				AdminView adminView = new AdminView();
				adminView.createContents();
				setCentralContent(adminView);
			}
		});
		projectsView.addComponent(button_Admin);
		projectsView.setComponentAlignment(button_Admin,
				Alignment.BOTTOM_CENTER);
		return projectsView;

	}

	protected Component createUserInfo() {
		Developer user = (Developer) PmApplication.getDefault().getUser();
		MenuBar profileMenu = new MenuBar();
		profileMenu.setStyleName(Constants.STYLE_HEADER);
		MenuItem rootItem = profileMenu.addItem(user.getName(), null);
		// rootItem.setStyleName(ExplorerLayout.STYLE_HEADER_PROFILE_MENU);

		// Change password
		rootItem.addItem(I18nManager.getMessage("password.change"),
				new Command() {
					private static final long serialVersionUID = -3071056323543851L;

					public void menuSelected(MenuItem selectedItem) {
						ResetPwdPage resetPwdPage = new ResetPwdPage();
						resetPwdPage.createContents();
						Window dvpDetailWindow = new Window(I18nManager
								.getMessage("password.change"));
						dvpDetailWindow.setModal(true);
						dvpDetailWindow.setWidth("800px");
						dvpDetailWindow.setHeight("80%");
						resetPwdPage.setWindow(dvpDetailWindow);
						dvpDetailWindow.addComponent(resetPwdPage);
						getWindow().addWindow(dvpDetailWindow);
					}
				});

		rootItem.addSeparator();

		// Logout
		rootItem.addItem(I18nManager.getMessage("header.logout"),
				new Command() {
					private static final long serialVersionUID = 1L;

					public void menuSelected(MenuItem selectedItem) {
						PmApplication.getDefault().close();
					}
				});

		// return userInfo;
		return profileMenu;
	}

	public void setCentralContent(Component mainContent) {
		central.removeAllComponents();
		central.addComponent(mainContent);
	}

	public void setFooter(Component footerContent) {
		footer.removeAllComponents();
		footer.addComponent(footerContent);
	}
}
